package com.dsteiner.widgets.client;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Date;

import com.dsteiner.widgets.client.Widgets.DateBoxExt;
import com.dsteiner.widgets.client.model.PrefilledTextBoxModel;
import com.dsteiner.widgets.client.resources.WidgetsResources;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.datepicker.client.DatePicker;
import com.google.gwt.user.datepicker.client.DateBox.DefaultFormat;


public class PrefilledDateBox extends PrefilledTextInput {
	
	public PrefilledDateBox(PrefilledTextBoxModel prefilledTextBoxModel) {
		this(prefilledTextBoxModel, new DateBoxExt());
	}
	
	public PrefilledDateBox(PrefilledTextBoxModel prefilledTextBoxModel, DatePicker datePicker, DateTimeFormat dateTimeFormat) {
		this(prefilledTextBoxModel, new DateBoxExt(datePicker, null, new DefaultFormat(dateTimeFormat)));
	}
	
	private PrefilledDateBox(final PrefilledTextBoxModel prefilledTextBoxModel, DateBoxExt dateBoxExt){
		super(prefilledTextBoxModel, dateBoxExt);
		addStyleName(WidgetsResources.INSTANCE.css().prefilledTextInputDate());
		final Timer clearFocusTimer = new Timer(){
			@Override
			public void run() {
				prefilledTextBoxModel.setHasFocus(false);
			}
		};
		dateBoxExt.addValueChangeHandler(new ValueChangeHandler<Date>(){
			public void onValueChange(ValueChangeEvent<Date> event) {
				clearFocusTimer.cancel();
				clearFocusTimer.schedule(50);
			}
		});
	}
}